package edu.ccut.saturn.studio.dialog.validatedialog;

import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableItem;

import edu.ccut.saturn.studio.logic.model.saturn.SaturnValidate;

public class MyCellModifierValidate implements ICellModifier {
	
	private TableViewer tableViewer;

	public MyCellModifierValidate(TableViewer tableViewer) {
		this.tableViewer = tableViewer;
	}

	public boolean canModify(Object element, String property) {
		return true;
	}

	public Object getValue(Object element, String property) {
		SaturnValidate o = (SaturnValidate) element;
		if (property.equals("id")) {
			return o.getId();
		} else if (property.equals("key")) {
			return o.getKey();
		} else if (property.equals("name")) {
			return o.getName();
		} else if (property.equals("class")){
			return o.getValidateClass();
		} else if (property.equals("JS")){
			return o.getJsMethodName();
		} else if (property.equals("customJS")){
			return o.getCustomJsMethod();
		} else /*if (property.equals("validateErrInfo")){
			return o.getErrorInfo();
		} else*/ if (property.equals("description")){
			return o.getDescription();
		}
		throw new RuntimeException("" + property); 
	}

	public void modify(Object element, String property, Object value) {
		TableItem item = (TableItem) element;
		SaturnValidate o = (SaturnValidate) item.getData();
		if (property.equals("key")) {
			String newKey = (String) value;
			if (newKey.equals("")){
				return;
			}
			o.setKey(newKey);			
		} else if (property.equals("name")) {
			String newName = (String) value;
			o.setName(newName);
		} else if (property.equals("class")) {
			String newClass = (String) value;
			o.setValidateClass(newClass);
		} else if (property.equals("JS")) {
			String newJS = (String) value;
			o.setJsMethodName(newJS);
		} else if (property.equals("customJS")) {
			String newCustomJS = (String) value;
			o.setCustomJsMethod(newCustomJS);
		} else /*if (property.equals("validateErrInfo")){
			String newValidateErrInfo = (String) value;
			o.setErrorInfo(newValidateErrInfo);
		} else*/ if(property.equals("description")){
			String newDescription = (String) value;
			o.setDescription(newDescription);
		} else {
			throw new RuntimeException("" + property); 
		}
		tableViewer.update(o, null);
	}

}
